package org.gvp.generator;

import org.gvp.generator.core.FileWriterHandler;
import org.gvp.generator.db.ConnectHandler;
import org.gvp.generator.log.LogFactory;
import org.gvp.generator.log.Logger;
import org.gvp.generator.props.CreateProps;
import org.gvp.generator.props.DatabaseProps;
import org.gvp.generator.props.ProjectProps;
import org.gvp.generator.table.TableHandler;
import org.gvp.generator.table.TableInfo;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;

public class Generator {
    private final Logger log = LogFactory.getLogger(this.getClass());
    private DatabaseProps database;
    private ProjectProps project;
    private CreateProps create;
    private Connection connection;

    public Generator(DatabaseProps database, ProjectProps project, CreateProps create) {
        this.database = database;
        this.project = project;
        this.create = create;
        log.debug("初始化代码生成器...");
    }

    /**
     * 根据数据表生产模板代码
     *
     * @param tableName
     */
    public void generatorCodeFormTableName(String tableName) {
        log.debug("准备生产模版代码,数据表名字: " + tableName);
        this.connection = new ConnectHandler(this.database).getConnection();
        log.debug("获取数据库连接对象",connection);
        log.debug("根据数据库连接对象和数据表名字构建数据表和数据列对象");
        TableInfo tableInfo = new TableHandler(this.connection, database.getDbName(),tableName).getTableInfo();
        this.project.buildEntityImportName(tableInfo.getUpperName());
        this.project.buildBasePackage();
        this.parsePath();
        log.debug("生成完整实体类包导入模板: ",this.project.getEntityImport());
        log.debug("生成基础包导入前缀: ",this.project.getBasePackageImport());
        log.debug("关闭数据库连接");
        try {
            this.connection.close();
        } catch (SQLException e) {
            log.error("关闭数据库连接失败");
            throw new RuntimeException(e);
        }
        log.debug("写入模版代码到文件...");
        new FileWriterHandler(this.project,this.create,tableInfo).write();
        log.debug("模版代码写入完成");
    }

    private void parsePath() {
        String javaPath = System.getProperty("user.dir") + File.separator +
                "src" + File.separator +
                "main" + File.separator +
                "java" + File.separator +
                this.project.getGroupId().replace(".", File.separator) +
                File.separator +
                this.project.getModuleName() +
                File.separator;
        log.debug("构建项目类路径: " + javaPath);
        String resourcesPath = System.getProperty("user.dir") + File.separator +
                "src" + File.separator +
                "main" + File.separator +
                "resources" + File.separator;
        log.debug("构建项目资源路径: " + resourcesPath);
        this.project.setJavaPath(javaPath);
        this.project.setResourcePath(resourcesPath);
    }

    /**
     * 获取生成器实例
     *
     * @param database 数据库配置信息
     * @param project  项目配置信息
     * @param create   生成文件配置信息
     */
    public static Generator getInstance(DatabaseProps database, ProjectProps project, CreateProps create) {
        return new Generator(database,project,create);
    }

}